package ce.com.cenewbluesdk.entity.k6;

import android.os.Parcel;
import android.os.Parcelable;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.Arrays;

import ce.com.cenewbluesdk.CEBC;
import ce.com.cenewbluesdk.entity.BaseData;
import ce.com.cenewbluesdk.entity.CEDevData;
import ce.com.cenewbluesdk.uitl.Util;

/**
 * Created by Administrator on 2015/11/28 0028.
 */
public class K6_Send_Watch_Face_And_Notification_Set extends BaseData implements Parcelable {
    public static final int  dateRealySize=9;
    public K6_Send_Watch_Face_And_Notification_Set(byte[] send_byte) {
        this.send_byte = send_byte;
    }
    private byte[] send_byte;

    protected K6_Send_Watch_Face_And_Notification_Set(Parcel in) {
        send_byte = in.createByteArray();
    }

    public static final Creator<K6_Send_Watch_Face_And_Notification_Set> CREATOR = new Creator<K6_Send_Watch_Face_And_Notification_Set>() {
        @Override
        public K6_Send_Watch_Face_And_Notification_Set createFromParcel(Parcel in) {
            return new K6_Send_Watch_Face_And_Notification_Set(in);
        }

        @Override
        public K6_Send_Watch_Face_And_Notification_Set[] newArray(int size) {
            return new K6_Send_Watch_Face_And_Notification_Set[size];
        }
    };

    public byte[] getBytes() {

//        return new byte[]{(byte) 0xff,0, (byte) 0xff,0};

        byte[] send=new byte[16];
        System.arraycopy(send_byte,0,send,0,send_byte.length);
        return send;
    }

    public static int getItemSize() {
        return 16;
    }

    /**
     * 现在有效数据的实际长度
     * @return
     */
    public static int getRealySize(){
        return dateRealySize;
    }


    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeByteArray(send_byte);
    }

    @Override
    public CEDevData toCEDevData() {
        CEDevData data=new CEDevData(CEBC.K6.CMD_TYPE_SEND,CEBC.K6.DATA_TYPE_WATCHFACE_MENU_SELECT);
        data.setItemL(getItemSize());
        data.setItemNumber(1);
        data.setData(getBytes());
        return data;
    }

    /**
     * 发送手表首页设置
     *
     * @param jsonArray
     */

    public static K6_Send_Watch_Face_And_Notification_Set createWatchFace(JSONArray jsonArray) {

        int[] ints1 = new int[8];
        int[] ints2 = new int[8];
        int[] ints3 = new int[8];
        int[] ints4 = new int[8];
        ints1[0] = 1;
        for (int i = 0; i < jsonArray.length(); i++) {
            try {
                if (i < 7) {
                    ints1[i + 1] = jsonArray.getInt(i);
                } else if (i < 15) {
                    ints2[i + 1 - 8] = jsonArray.getInt(i);
                } else if (i < 23) {
                    ints3[i + 1 - 16] = jsonArray.getInt(i);
                }else if(i<31){
                    ints4[i + 1 - 24] = jsonArray.getInt(i);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
        return new K6_Send_Watch_Face_And_Notification_Set(
                new byte[]{Util.Convert(ints1), Util.Convert(ints2), Util.Convert(ints3), Util.Convert(ints4)});

    }

    @Override
    public String toString() {
        return "K6_Send_Watch_Face_And_Notification_Set{" +
                "send_byte=" + CEBC.byte2hex(send_byte) +
                '}';
    }
}
